
var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

layer.ready(function () {
    //获取所有用户信息
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $("#salesmanCode").append("<option value=\"" + data.data[i].code + "\">" + data.data[i].name + "</option>");
                }
                form.render('select');
            }
        }
    });
});

let initData = [];
table.render({
    elem: '#mains',
    data: initData
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'purchaseNo', width: 120, title: '采购合同号', sort: true}
        ,{field:'contractNo',width:120, title:'合同编号',sort:true}
        ,{field:'supplierName',width:120, title:'供应商名称',sort:true}
        ,{field:'currency',width:60, title:'币种',sort:true}
        ,{field:'berPersonName',width:100, title:'业务员',sort:true}
        ,{field:'operation',width:100, title:'操作员',sort:true}
        ,{field:'totalPurchaseAmount',width:160, title:'采购合同总额',sort:true}
        ,{field:'earnestRate',width:160, title:'合同预付款比例',sort:true}
        ,{field:'earnestMoney',width:160, title:'合同预付款总额',sort:true}
        ,{field:'amountPaid',width:160, title:'合同已付款金额',sort:true}
        ,{field:'unpaidAmount',width:160, title:'合同未付款金额',sort:true}
        ,{field:'totalApplication', width: 120, title: '已申请合计', sort: true}
        ,{field:'signedCompanyName',width:180, title:'制单公司',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 600
    ,page: true
});

form.on('submit(submitform)', function(data){
    table.reload('mainstableid', {
        page: {
            limit: 10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
        ,url:"payment/getImportPurchaseByPage"
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    //第一个表格选中的数据
    let productData =   table.checkStatus("mainstableid").data;
    if(productData.length == 0){
        layer.msg("请选择数据")
    }else{
        //获取父页面明细数据
        let oldData =  parent.table.cache["maininformationtableid"];
        let num = oldData.length+1;
        $.each(productData, function (index, value) {
            //剔除重复数据
            let flag = true;
            if(oldData.length > 0){
                for(var i = 0; i < oldData.length; i++){
                    if(oldData[i]["stockInfoId"] == productData[index].stockInfoId){
                        flag = false;
                        break;
                    }
                }
            }
            if(flag){
                //添加临时id
                productData[index]["tempId"] = GenNonDuplicateID(3) + num;
                //登记预付款
                productData[index]["type"] = 1;
                num++;
                //不存在重复数据,添加数据
                oldData.push(productData[index]);
            }
        });
        parent.table.reload('maininformationtableid',{data : oldData, limit: oldData.length});
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },)
    }
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})

//生成临时id
function GenNonDuplicateID(randomLength){
    return Number(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}